home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 242_01 / a611.c < prev    next >
Text File  |  1989-01-11  |  12KB  |  475 lines

  1. /*
  2.     HEADER:        CUG242;
  3.     TITLE:        68HC11 Cross-Assembler (Portable);
  4.     FILENAME:    A611.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    A611.H
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.              68HC11 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1985,1987 William C. Colley, III
  15.  
  16. This file contains the main program and line assembly routines for the
  17. assembler.  The main program parses the command line, feeds the source lines
  18. to the line assembly routine, and sends the results to the listing and object
  19. file output routines.  It also coordinates the activities of everything.  The
  20. line assembly routines uses the expression analyzer and the lexical analyzer
  21. to parse the source line and convert it into the object bytes that it
  22. represents.
  23. */
  24.  
  25. /*  Get global goodies:  */
  26.  
  27. #include "a611.h"
  28.  
  29. /*  Define global mailboxes for all modules:                */
  30.  
  31. char errcode, line[MAXLINE + 1], title[MAXLINE];
  32. int pass = 0;
  33. int eject, filesp, forwd, listhex;
  34. unsigned  address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
  35. FILE *filestk[FILES], *source;
  36. TOKEN token;
  37.  
  38. /*  Mainline routine.  This routine parses the command line, sets up    */
  39. /*  the assembler at the beginning of each pass, feeds the source text    */
  40. /*  to the line assembler, feeds the result to the listing and hex file    */
  41. /*  drivers, and cleans everything up at the end of the run.        */
  42.  
  43. static int done, ifsp, off;
  44.  
  45. void main(argc,argv)
  46. int argc;
  47. char **argv;
  48. {
  49.     SCRATCH unsigned *o;
  50.     int newline();
  51.     void asm_line();
  52.     void lclose(), lopen(), lputs();
  53.     void hclose(), hopen(), hputc();
  54.     void error(), fatal_error(), warning();
  55.  
  56.     printf("68HC11 Cross-Assembler (Portable) Ver 0.1\n");
  57.     printf("Copyright (c) 1985, 1987 William C. Colley, III\n\n");
  58.  
  59.     while (--argc > 0) {
  60.     if (**++argv == '-') {
  61.         switch (toupper(*++*argv)) {
  62.         case 'L':   if (!*++*argv) {
  63.                 if (!--argc) { warning(NOLST);  break; }
  64.                 else ++argv;
  65.                 }
  66.                 lopen(*argv);
  67.                 break;
  68.  
  69.         case 'O':   if (!*++*argv) {
  70.                 if (!--argc) { warning(NOHEX);  break; }
  71.                 else ++argv;
  72.                 }
  73.                 hopen(*argv);
  74.                 break;
  75.  
  76.         default:    warning(BADOPT);
  77.         }
  78.     }
  79.     else if (filestk[0]) warning(TWOASM);
  80.     else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
  81.     }
  82.     if (!filestk[0]) fatal_error(NOASM);
  83.  
  84.     while (++pass < 3) {
  85.     fseek(source = filestk[0],0L,0);  done = off = FALSE;
  86.     errors = filesp = ifsp = pagelen = pc = 0;  title[0] = '\0';
  87.     while (!done) {
  88.         errcode = ' ';
  89.         if (newline()) {
  90.         error('*');
  91.         strcpy(line,"\tEND\n");
  92.         done = eject = TRUE;  listhex = FALSE;
  93.         bytes = 0;
  94.         }
  95.         else asm_line();
  96.         pc = word(pc + bytes);
  97.         if (pass == 2) {
  98.         lputs();
  99.         for (o = obj; bytes--; hputc(*o++));
  100.         }
  101.     }
  102.     }
  103.  
  104.     fclose(filestk[0]);  lclose();  hclose();
  105.  
  106.     if (errors) printf("%d Error(s)\n",errors);
  107.     else printf("No Errors\n");
  108.  
  109.     exit(errors);
  110. }
  111.  
  112. /*  Line assembly routine.  This routine gets expressions and tokens    */
  113. /*  from the source file using the expression evaluator and lexical    */
  114. /*  analyzer, respectively.  It fills a buffer with the machine code    */
  115. /*  bytes and returns nothing.                        */
  116.  
  117. static char label[MAXLINE];
  118. static int ifstack[IFDEPTH] = { ON };
  119.  
  120. static OPCODE *opcod;
  121.  
  122. void asm_line()
  123. {
  124.     SCRATCH int i;
  125.     int isalph(), popc();
  126.     OPCODE *find_code(), *find_operator();
  127.     void do_label(), flush(), normal_op(), pseudo_op();
  128.     void error(), pops(), pushc(), trash();
  129.  
  130.     address = pc;  bytes = 0;  eject = forwd = listhex = FALSE;
  131.     for (i = 0; i < BIGINST; obj[i++] = NOP);
  132.  
  133.     label[0] = '\0';
  134.     if ((i = popc()) != ' ' && i != '\n') {
  135.     if (isalph(i)) {
  136.         pushc(i);  pops(label);
  137.         if (find_operator(label)) { label[0] = '\0';  error('L'); }
  138.     }
  139.     else {
  140.         error('L');
  141.         while ((i = popc()) != ' ' && i != '\n');
  142.     }
  143.     }
  144.  
  145.     trash(); opcod = NULL;
  146.     if ((i = popc()) != '\n') {
  147.     if (!isalph(i)) error('S');
  148.     else {
  149.         pushc(i);  pops(token.sval);
  150.         if (!(opcod = find_code(token.sval))) error('O');
  151.     }
  152.     if (!opcod) { listhex = TRUE;  bytes = BIGINST; }
  153.     }
  154.  
  155.     if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
  156.     else if (off) { listhex = FALSE;  flush();  return; }
  157.  
  158.     if (!opcod) { do_label();  flush(); }
  159.     else {
  160.     listhex = TRUE;
  161.     if (opcod -> attr & PSEUDO) pseudo_op();
  162.     else normal_op();
  163.     while ((i = popc()) != '\n') if (i != ' ') error('T');
  164.     }
  165.     source = filestk[filesp];
  166.     return;
  167. }
  168.  
  169. static void flush()
  170. {
  171.     while (popc() != '\n');
  172. }
  173.  
  174. static void do_label()
  175. {
  176.     SCRATCH SYMBOL *l;
  177.     SYMBOL *find_symbol(), *new_symbol();
  178.     void error();
  179.  
  180.     if (label[0]) {
  181.     listhex = TRUE;
  182.     if (pass == 1) {
  183.         if (!((l = new_symbol(label)) -> attr)) {
  184.         l -> attr = FORWD + VAL;
  185.         l -> valu = pc;
  186.         }
  187.     }
  188.     else {
  189.         if (l = find_symbol(label)) {
  190.         l -> attr = VAL;
  191.         if (l -> valu != pc) error('M');
  192.         }
  193.         else error('P');
  194.     }
  195.     }
  196. }
  197.  
  198. static void normal_op()
  199. {
  200.     SCRATCH unsigned attrib, operand, *o;
  201.     static unsigned prebyte[2][4] = {
  202.     { 0x00, 0x00, 0x18, 0x1a },
  203.     { 0x18, 0xcd, 0x1a, 0xcd }
  204.     };
  205.     unsigned expr();
  206.     TOKEN *lex();
  207.     void do_label(), error(), s_error(), unlex();
  208.  
  209.     do_label();
  210.  
  211.     o = obj;
  212.     if (operand = prebyte[0][(attrib = opcod -> attr) & PREBYTE])
  213.     *o++ = operand;
  214.     *o++ = opcod -> valu;
  215.     while (attrib & ~PREBYTE) {
  216.     switch (lex() -> attr & TYPE) {
  217.         case IMM:    if (!(attrib & IMMED)) { s_error();  return; }
  218.                 operand = expr();
  219.             if (attrib & IMM_16) *o++ = high(operand);
  220.             else if (operand > 0xff && operand < 0xff80) {
  221.                 error('V');  operand = 0;
  222.             }
  223.             *o++ = low(operand);  attrib = 0;  break;
  224.  
  225.         case REG:    operand = 0;
  226. do_indx:        if (!(attrib & INDX)) { s_error();  return; }
  227.             if (token.valu == (*obj == 0x18 ? 'X' : 'Y')) {
  228.                 if ((attrib & PREBYTE) < 2) *o++ = *obj;
  229.                 *obj = prebyte[1][attrib & PREBYTE];
  230.             }
  231.             switch (*--o) {
  232.                 case 0x12:
  233.                 case 0x13:    *o++ += 0x0c;  break;
  234.  
  235.                 case 0x14:
  236.                 case 0x15:    *o++ += 0x08;  break;
  237.             
  238.                 default:    *o++ += 0x20;  break;
  239.             }
  240.             if (operand > 0xff && operand < 0xff80) {
  241.                 error('V');  operand = 0;
  242.             }
  243.             *o++ = low(operand);
  244.             if ((attrib &= REL + MASK) & (REL + MASK)) {
  245.                 if ((lex() -> attr & TYPE) != SEP) {
  246.                 s_error();  return;
  247.                 }
  248.             }
  249.             break;
  250.  
  251.         default:    unlex();  operand = expr();
  252.             if ((token.attr & TYPE) == SEP) {
  253.                 if ((lex() -> attr & TYPE) == REG) goto do_indx;
  254.                 unlex();
  255.             }
  256.             switch (attrib & (DIR + EXT)) {
  257.                 case DIR + EXT: if (!forwd && operand <= 0xff)
  258.                         goto do_dir;
  259.                     
  260.                 case EXT:        o[-1] += 0x30;
  261.                         *o++ = high(operand);
  262.                         *o++ = low(operand);
  263.                         attrib = 0;
  264.                         break;
  265.  
  266.                 case DIR:        if (operand > 0xff) {
  267.                         error('V');  operand = 0;
  268.                         }
  269. do_dir:                        if (o[-1] >= 0x40) o[-1] += 0x10;
  270.                         *o++ = low(operand);
  271.                         attrib &= REL + MASK;
  272.                         break;
  273.  
  274.                 default:        if (attrib & MASK) {
  275.                         if (operand > 0xff) {
  276.                             error('V');  operand = 0;
  277.                         }
  278.                         *o++ = low(operand);
  279.                         attrib &= REL;
  280.                         }
  281.                         else {
  282.                         if ((operand = word(operand -
  283.                             (pc + (o + 1 - obj))))
  284.                             > 0x7f &&
  285.                             operand < 0xff80) {
  286.                             error('B');
  287.                             operand = obj - (o + 1);
  288.                         }
  289.                         *o++ = low(operand);
  290.                         attrib = 0;
  291.                         }
  292.                         break;
  293.             }
  294.     }
  295.     }
  296.     bytes = o - obj;  return;
  297. }
  298.  
  299. void s_error()
  300. {
  301.     SCRATCH unsigned *o;
  302.  
  303.     error('S');  bytes = BIGINST;
  304.     for (o = obj; o < obj + BIGINST; *o++ = NOP);
  305. }
  306.  
  307. static void pseudo_op()
  308. {
  309.     SCRATCH char *s;
  310.     SCRATCH unsigned *o, u;
  311.     SCRATCH SYMBOL *l;
  312.     unsigned expr();
  313.     SYMBOL *find_symbol(), *new_symbol();
  314.     TOKEN *lex();
  315.     void do_label(), error(), fatal_error(), hseek(), unlex();
  316.  
  317.     o = obj;
  318.     switch (opcod -> valu) {
  319.     case ELSE:  listhex = FALSE;
  320.             if (ifsp) off = (ifstack[ifsp] = -ifstack[ifsp]) !=